//
//  XMConOpenBoxRuleView.m
//  XunMi
//
//  Created by MacBook on 2024/4/30.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMConOpenBoxRuleView.h"
#import "XMConOpenBoxRuleCell.h"
#import "XMOpenboxModel.h"
#import "XMGiftModel.h"


@interface XMConOpenBoxRuleView () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *titleLa;
@property (weak, nonatomic) IBOutlet UILabel *descLa;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionH;

@end

@implementation XMConOpenBoxRuleView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.collectionView.layer.borderWidth = 1;
    self.collectionView.layer.borderColor = kHEX(@"#E0E0E0").CGColor;
    self.collectionView.contentInset = UIEdgeInsetsMake(6, 6, 6, 6);
    
    UICollectionViewFlowLayout *la = [[UICollectionViewFlowLayout alloc] init];
    la.itemSize = CGSizeMake((295 - 12 - 44) / 2, 24);
    la.minimumLineSpacing = 0;
    la.minimumInteritemSpacing = 0;
    [self.collectionView setCollectionViewLayout:la];
    [self.collectionView registerNib:kNibWithName(kStringFromClass(XMConOpenBoxRuleCell)) forCellWithReuseIdentifier:kStringFromClass(XMConOpenBoxRuleCell)];
}

- (void)setModel:(XMOpenboxModel *)model {
    _model = model;
    self.titleLa.text = model.title;
    self.textView.text = model.prove;
    
    double number = self.model.gifts.count / 2.0;
    double roundedNumber = ceil(number);
    self.collectionH.constant = roundedNumber * 24 + (roundedNumber - 1) * 0;
    
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.gifts.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMConOpenBoxRuleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kStringFromClass(XMConOpenBoxRuleCell) forIndexPath:indexPath];
    XMGiftModel *model = [self.model.gifts safe_objectAtIndex:indexPath.item];
    [cell.imgV sd_setImageWithURL:kStringToUrl(model.image.file_url) placeholderImage:kDefaultCoverImg];
    cell.nameLa.text = kStringFormat(@"%@%@", model.name, model.probability);
    return cell;
}


- (IBAction)close:(id)sender {
    self.hidden = YES;
}



@end
